package com.hlf.modules.merchant.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.hlf.common.exception.BusinessException;
import com.hlf.common.utils.IdWorkerFactory;
import com.hlf.common.utils.PageUtils;
import com.hlf.common.utils.Query;
import com.hlf.modules.h5.interceptor.AuthorizationInterceptor;
import com.hlf.modules.merchant.goods.entity.Goods;
import com.hlf.modules.merchant.goods.mapper.GoodsMapper;
import com.hlf.modules.merchant.goods.service.GoodsService;
import com.hlf.modules.sys.entity.File;
import com.hlf.modules.sys.entity.SysUserEntity;
import com.hlf.modules.sys.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private FileService fileService;

    /**
     * 查询单个商品
     *
     * @param id
     * @return
     */
    @Override
    public Goods queryObject(Long id) {
        return goodsMapper.queryObject(id);
    }

    @Override
    public List<Goods> queryList(Map<String, Object> map) {
        return null;
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return 0;
    }

    /**
     * 保存
     *
     * @param goods
     */
    @Override
    public void save(Goods goods) {
        goodsMapper.save(goods);
    }

    /**
     * 选择性新增
     *
     * @param goods
     * @return
     */
    @Override
    public int insertSelective(Goods goods) {
        return goodsMapper.insertSelective(goods);
    }

    /**
     * 分页查询
     */
    @Override
    public PageUtils queryListForPage(Map<String, Object> map) {
        Query query = new Query(map);
        int total = goodsMapper.queryTotal(query);
        List<Goods> goodsList = null;
        if (total > 0) {
            goodsList = goodsMapper.queryList(query);
        }
        return new PageUtils(goodsList, total, query.getLimit(), query.getPage());
    }

    /**
     * 批量逻辑删除
     *
     * @param goodsIdList
     * @param userId
     * @return
     */
    @Override
    public int batchUpdateDel(List<Long> goodsIdList, Long userId) {
        return goodsMapper.batchUpdateDel(goodsIdList, userId);
    }

    /**
     * 修改
     *
     * @param goods
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(Goods goods) {
        return goodsMapper.updateByPrimaryKeySelective(goods);
    }

    /**
     * 根据门店id查商品集合
     *
     * @param storeId
     * @return
     */
    @Override
    public List<Goods> queryListForStoreId(Long storeId) {
        return goodsMapper.queryListForStoreId(storeId);
    }

    /**
     * 查询本店茶位费
     *
     * @param storeId
     * @return
     */
    @Override
    public Goods queryTeaForStoreId(Long storeId) {
        return goodsMapper.queryTeaForStoreId(storeId);
    }

    /**
     * 创建
     *
     * @param merchantId
     * @param name
     * @param goodsTypeId
     * @param sellPrice
     * @param remainQty
     * @param goodsImgFilesJson
     * @param dailyStock
     * @param user
     */
    @Override
    @Transactional
    public void create(String merchantId, String name, String goodsTypeId, String sellPrice, String remainQty, String goodsImgFilesJson, String dailyStock, SysUserEntity user) {
        Long userId = user.getUserId();
        long id = IdWorkerFactory.nextId();
        List<File> goodsImgs = null;
        if (goodsImgFilesJson != null && goodsImgFilesJson.length() > 0) {
            goodsImgs = JSON.parseArray(goodsImgFilesJson, File.class);
            for (File goodsImg : goodsImgs) {
                goodsImg.setInsertBy(userId);
                goodsImg.setForeignId(id);
            }
            fileService.inserts(goodsImgs);
        }
        Goods goods = new Goods();
        goods.setId(id);
        goods.setMerchantId(Long.parseLong(merchantId));
        goods.setStoreId(Long.parseLong(user.getCurrentStoreId()));
        goods.setGoodsImgs(goodsImgs);
        goods.setGoodsType(Long.parseLong(goodsTypeId));
        goods.setName(name);
        goods.setNum(IdWorkerFactory.nextId());
        goods.setSellPrice(Integer.parseInt(sellPrice));
        goods.setInitQty(Integer.parseInt(remainQty));
        goods.setRemainQty(Integer.parseInt(remainQty));
        goods.setInsertBy(userId);
        goods.setDailyStock(Byte.parseByte(dailyStock));

        insertSelective(goods);
    }

    /**
     * 修改
     *
     * @param goodsId
     * @param name
     * @param goodsTypeId
     * @param sellPrice
     * @param remainQty
     * @param dailyStock
     * @param goodsImgFilesJson
     * @param request
     */
    @Override
    @Transactional
    public void updateGoods(String goodsId, String name, String goodsTypeId, String sellPrice, String remainQty, String dailyStock, String goodsImgFilesJson, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute(AuthorizationInterceptor.LOGIN_USER_KEY);
        Long id = Long.parseLong(goodsId);
        Goods goods = new Goods();
        goods.setId(id);
        goods.setName(name);
        goods.setGoodsType(Long.parseLong(goodsTypeId));
        goods.setSellPrice(Integer.parseInt(sellPrice));
        goods.setRemainQty(Integer.parseInt(remainQty));
        goods.setDailyStock(Byte.parseByte(dailyStock));
        goods.setLastUpdateBy(userId);

        List<File> goodsImgs;
        if (goodsImgFilesJson != null && goodsImgFilesJson.length() > 0) {
            goodsImgs = JSON.parseArray(goodsImgFilesJson, File.class);
            for (File goodsImg : goodsImgs) {
                goodsImg.setInsertBy(userId);
                goodsImg.setForeignId(id);
            }
            fileService.inserts(goodsImgs);
        }
        updateByPrimaryKeySelective(goods);
    }

    /**
     * 售罄/上下架/修改茶位费
     * @param goodsId
     * @param status
     * @param request
     * @param teaPri
     */
    @Override
    public void updateGoodsStatus(String goodsId, String status, HttpServletRequest request, String teaPri) {
        Long userId = (Long) request.getAttribute(AuthorizationInterceptor.LOGIN_USER_KEY);
        Goods goods = new Goods();
        goods.setId(Long.parseLong(goodsId));
        goods.setLastUpdateBy(userId);

        if (status != null && status.length() > 0) {
            goods.setStatus(Byte.parseByte(status));
        } else if (teaPri != null && teaPri.length() > 0) {
            goods.setSellPrice(Integer.parseInt(teaPri));
        } else {
            throw new BusinessException("数据不能为空!");
        }

        int i = updateByPrimaryKeySelective(goods);
        if (i == 0) {
            throw new BusinessException("更改失败!");
        }
    }

}
